<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'image-form',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
    ),
    //'enableAjaxValidation'=>true,
    'htmlOptions'=>array('enctype' => 'multipart/form-data', 'class' => 'custom'),
)); ?>
<div style="width: 368px">
    <div>
        <div style="float: right;"><span id="name_char_count"></span> <?php echo Yii::t('imageModule.app', 'Chars')?> </div>
        <script>
            $(function(){
                $("#Image_name").keyup(function(){
                    $('#name_char_count').text($(this).val().length);
                }).keyup();
            });
        </script>
        <?php echo $form->labelEx($model,'name');?><br>
        <?php echo $form->textField($model,'name', array('maxlength' => 70, 'class' => 'text')); ?>
        <?php echo $form->error($model,'name'); ?>
    </div>
    <?php if(!$model->isNewRecord && $model->image):?> 
        <br> 
        <div> 
        <?php echo $form->labelEx($model, 'image')?><br>
        <?php if($model->image):?>
            <a title="<?php echo Yii::t('app', 'Click to view full-size')?>" href="<?php echo $model->imageUrl?>" target="_blank">
                <img height="100" class="border" src="<?php echo $model->imageThumbUrl?>"/>
            </a>
            <br>
        <?php endif?>
        </div>
    <?php endif?>
    
    <div style="margin-top: 10px;">
        <style>
        #Image_upload_method label{
            vertical-align: 2px;
        }
        </style>
        <div style="float: left;"> 
            <?php echo $form->labelEx($model, 'upload_method', array('style' => 'vertical-align: -3px; margin-right: 10px'))?>
        </div>
        <div  style="float: left;">
        <?php echo $form->radioButtonList($model,'upload_method',array(
            'file'=> Yii::t('imageModule.app', 'From computer'),
            'url'=> Yii::t('imageModule.app', 'From url'),
        ), array(
            'template'  => '<div style="display: inline;">{input} {label}</div>',
            'separator' => '',
            'labelOptions'=>array('style'=>'display:inline'),
        ));?>
        </div>
        <div class="clear"></div>
    </div> 
    <div id="image_file">
        <?php echo $form->fileField($model,'image_file', array('size' => 45, 'class' => 'text')); ?>
        <div><?php echo Yii::t('imageModule.app', 'Support only: jpg, gif, png. Size < 4Mb')?></div>
        <?php echo $form->error($model,'image_file'); ?>
    </div>
    
    <div id="image_url" style="display: none;">
        <?php echo $form->textField($model,'image_url', array('placeholder' => Yii::t('app', 'Enter a image url'), 'class' => 'text', 'style' => 'width: 360px;')); ?>
        <div>VD: http://domain.com/path/image.jpg</div>
        <?php echo $form->error($model,'image_url'); ?>
    </div>
    <?php echo $form->error($model,'upload_method'); ?>
    <br> 
    <div>
        <div style="float: right;"><span id="desc_char_count"></span> <?php echo Yii::t('imageModule.app', 'Chars')?> </div>
        <script>
            $(function(){
                $("#Image_desc").keyup(function(){
                    $('#desc_char_count').text($(this).val().length);
                }).keyup();
            });
        </script>
        <?php echo $form->labelEx($model,'desc');?><br>
        <?php echo $form->textArea($model,'desc', array('maxlength' => 160)); ?>
        <?php echo $form->error($model,'desc'); ?>
    </div>
    

    <?php if($this->user->isMod):?>
    <div>
        <br>
        <?php echo $form->labelEx($model,'crop_bottom');?><br>
        <?php echo $form->dropDownList($model,'crop_bottom', array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5), array('empty' => 'None')); ?>
    </div>
    <br> 
    <div>
        <?php echo $form->labelEx($model,'status'); ?><br>
        <?php echo $form->dropDownList($model, 'status', Image::model()->statusData);?>
        <?php echo $form->error($model,'status'); ?>
    </div>
    <?php endif?>

    <div>
        <br>
        <button type="submit"><?php echo Yii::t('app', Yii::t('imageModule.app', $model->isNewRecord ? 'Create' : 'Update' ))?></button>
    </div>
</div>
<?php $this->endWidget(); ?>


<script>
$(function(){   
    if($('input[name="Image[upload_method]"]:checked').val() == 'file'){
        $('#image_file').show();
    }else{
        $('#image_url').show();
    }
    $('input[name="Image[upload_method]"]').change(function(){
         var method = $(this).val();
         if(method == 'file'){
             $('#image_url').hide();
             $('#image_file').show();
         }else{
             $('#image_file').hide();
             $('#image_url').show();
         }
    }); 
});
</script>
<br>
<br>
<br>
<br>
<hr>
<p>Chú ý: Miêu tả cho ảnh không bắt buộc phải điền. Tuy nhiên nếu miêu tả hay và trên 120 ký tự sẽ được thưởng 3 point. 
Chi tiết về điểm và cấp độ bạn có thể xem tại <a href="<?php echo $this->createUrl('/web/page/point')?>">Bảng điểm cấp độ</a></p>